package com.coda.service.impl;

import com.coda.Constant.StatusConstant;
import com.coda.common.PageResult;
import com.coda.dto.PageDto;
import com.coda.entity.Product;
import com.coda.entity.Store;
import com.coda.mapper.ProductMapper;
import com.coda.mapper.StoreMapper;
import com.coda.service.ProductService;
import com.coda.service.UploadService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private UploadServiceImpl uploadService;

    /**
     * 分页查询商品列表
     * @param pageDto
     * @param product
     * @return
     */
    @Override
    public PageResult productPage(PageDto pageDto, Product product) {
        //设置分页参数
        PageHelper.startPage(pageDto.getPageNum(),pageDto.getPageSize());
        //分页查询
        Page<Product> page = productMapper.getProductPage(product);
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }

    /**
     * 添加商品
     * @param product
     */
    @Override
    public void addProduct(Product product) {
        String imageUrl = uploadService.getImageUrl();
        product.setImgs(imageUrl);
        product.setUpDownState(StatusConstant.USER_STATE_NOT_PASS);
        //添加商品
        productMapper.addProduct(product);
    }

    /**
     * 修改商品状态
     * @param product
     */
    @Override
    public void updateState(Product product) {
        productMapper.update(product);
    }

    /**
     * 删除商品
     * @param id
     */
    @Override
    public void deleteProductById(Long id) {
        List<Long> ids = new ArrayList<>();
        ids.add(id);
        productMapper.deleteProductById(ids);
    }

    /**
     * 根据id批量删除商品
     * @param ids
     */
    @Override
    public void deleteProductsByIds(List<Long> ids) {
        productMapper.deleteProductById(ids);
    }

    /**
     * 根据id修改商品
     * @param product
     */
    @Override
    public void update(Product product) {
        productMapper.update(product);
    }



}
